package com.vframework.system.menu.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.vframework.system.menu.model.MenuBean;

public class MenuTree {
    
    public static List<MenuBean> buildDivMenuTreeData(List<MenuBean> menuList){
        List<MenuBean> resultList = new ArrayList<MenuBean>();
        Map<String, List<MenuBean>> map = new HashMap<String, List<MenuBean>>();
        for (MenuBean menu : menuList) {
            if ("0".equals(menu.getMenuLevel())) {////系统菜单  顶级菜单
                continue;
//                resultList.add(menu);
            } else if ("1".equals(menu.getMenuLevel())) {//系统管理   一级子菜单
                resultList.add(menu);
//                List<MenuBean> temp = map.get(menu.getMenuId()) == null ? new ArrayList<MenuBean>() : map.get(menu.getMenuId());
//                temp.add(menu);
//                map.put(menu.getMenuId(), temp);
            } else {
                List<MenuBean> temp = map.get(menu.getParentMenu()) == null ? new ArrayList<MenuBean>() : map.get(menu.getParentMenu());
                temp.add(menu);
                map.put(menu.getParentMenu(), temp);
            }
        }
        //拼装
        for (MenuBean menu: resultList) {
            menu.setChildren(map.get(menu.getMenuId()));
        }
        
        return resultList;
    }
    
	public static String buildDivMenuTree(List<MenuBean> menuList){
		StringBuffer  strTree= new StringBuffer();
		String menuLevel;
		String className;
		String menuName;
		if(menuList==null || menuList.size()==0){

			return "没有给用户分配菜单";
		}
		for(int i=0; i<menuList.size(); i++){
			MenuBean menu = menuList.get(i);
			menuLevel = menu.getMenuLevel();
			className = menu.getMenuImage();
			menuName = menu.getMenuName();
			if("0".equals(menuLevel)){//系统菜单  顶级菜单
				strTree.append("<div class=\""+className+"\"></div>");
				strTree.append("<div id=\"accordion\">");
			}else if("1".equals(menuLevel)){//系统管理   一级子菜单
				strTree.append("<h3><a href=\"#\">"+menu.getMenuName()+"</a></h3>");
				strTree.append("<div>");
				strTree.append(buildSubDivMenuTree(menuList,menu.getMenuId()));
				strTree.append("</div>");
			}
		}
		strTree.append("</div>");
		return strTree.toString();
	}
	public static String buildSubDivMenuTree(List<MenuBean> menuList,String menuId){
		StringBuffer  str= new StringBuffer();
		MenuBean menu = null;
		for(int i=0; i<menuList.size(); i++){
			menu = menuList.get(i);
			if(menuId.equals(menu.getParentMenu())){
				String menuPath = menu.getMenuPath();
				str.append("<p class=\""+menu.getMenuImage()+"\""
								+" onclick=\"return popupPage('"+menu.getMenuId()+"','"+menu.getMenuName()+"','"+menuPath+"','"+menu.getMenuTarget()+"');\""
								+" onmouseover=\"this.className='"+menu.getMenuImage()+" mouseOver'\""
								+" onmouseout=\"this.className='"+menu.getMenuImage()+"'\">"
								+menu.getMenuName()
							+"</p>");
			}
		}
		return str.toString();
		
	}
	
}
